import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-12-14
 * Time: 12:28
 */
class Solution1_5 {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        //定义两个指针
        int i = 0 , j = 0 , n = pushed.length;
        Stack<Integer> stack = new Stack<>();
        while(i < n){
            if(i < n && pushed[i] != popped[j]){
                stack.push(pushed[i]);
                i++;
            }else{
                //不相等的时候，先进栈
                stack.push(pushed[i]);
                // 出栈
                while(i < n && !stack.isEmpty() && stack.peek() == popped[j]){
                    stack.pop();
                    j++;
                }
                i++;
            }
        }
        return stack.isEmpty();
    }
}
public class Test1_5 {
}
